/*
 * =============================================================================
 * Copyright© by Benjamin Sempere,
 * All rights reserved.
 * =============================================================================
 * Author  : Benjamin Sempere
 * E-mail  : benjamin@sempere.org
 * Homepage: www.sempere.org
 * =============================================================================
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package org.sempere.gwt.toolbox.remoting.client.dto;

import java.util.ArrayList;
import java.util.Collection;

public abstract class AbstractDTOsGrid {

	private Collection<AbstractDTORow> rows = new ArrayList<AbstractDTORow>();

	/**
	 * Default constructor
	 */
	public AbstractDTOsGrid(Collection<AbstractDTORow> rows) {
		if (rows == null) {
			throw new IllegalArgumentException("Rows list cannot be null.");
		}

		this.rows = rows;
	}

	/**
	 * Return the name of the grid's columns
	 * 
	 * @return String[]
	 */
	public abstract String[] getColumnNames();

	/**
	 * Return the grid data
	 * 
	 * @return Object[][]
	 */
	public Object[][] getGridData() {
		Collection<Object[]> gridData = new ArrayList<Object[]>();

		int nbDataPerRow = 0;
		for (AbstractDTORow row : this.rows) {
			Object[] rowData = row.getRowData();

			nbDataPerRow = Math.max(nbDataPerRow, rowData.length);
			gridData.add(rowData);
		}

		return (Object[][]) gridData.toArray(new Object[nbDataPerRow][gridData.size()]);
	}

	public Collection<AbstractDTORow> getRows() {
		return rows;
	}

	public void setRows(Collection<AbstractDTORow> rows) {
		this.rows = rows;
	}
}
